/*
 * UeyeCameraImpl.cpp
 *
 *  Created on: 22.03.2013
 *      Author: OrGeJs
 */

#include "../header/UeyeCameraImpl.h"

UeyeCameraImpl::UeyeCameraImpl() {

	cameraHandle = (HIDS) 0;    // open next room
	is_InitCamera(&cameraHandle, NULL);    // init room
	is_GetSensorInfo(cameraHandle, &sInfo);
	if (cameraHandle != 0) {
		is_ExitCamera (cameraHandle);
	}

	params.width = sInfo.nMaxWidth;
	params.height = sInfo.nMaxHeight;
	params.id = cameraHandle;

}

UeyeCameraImpl::~UeyeCameraImpl() {

	int retInt = is_ExitCamera(cameraHandle);
		if (retInt != IS_SUCCESS) {
			throw UeyeOpenCVException(cameraHandle, retInt);
		}

}

ErrorCode UeyeCameraImpl::openCamera() {

	mat = cv::Mat(params.height, params.width, CV_8UC1);
	char* ppcImgMem;
	int pid;
	INT nAOISupported = 0;
	double on = 1;
	double empty;
	int retInt = IS_SUCCESS;
	retInt = is_InitCamera(&cameraHandle, 0);
	if (retInt != IS_SUCCESS) {
		throw UeyeOpenCVException(cameraHandle, retInt);
	}
	retInt = is_SetColorMode(cameraHandle, IS_CM_SENSOR_RAW8);

	if (retInt != IS_SUCCESS) {
		throw UeyeOpenCVException(cameraHandle, retInt);

	}
	cout << "color mode set" << endl;
	retInt = is_ImageFormat(cameraHandle, IMGFRMT_CMD_GET_ARBITRARY_AOI_SUPPORTED,
			(void*) &nAOISupported, sizeof(nAOISupported));
	if (retInt != IS_SUCCESS) {
		throw UeyeOpenCVException(cameraHandle, retInt);
	}
	cout << "image format set" << endl;
	retInt = is_AllocImageMem(cameraHandle, params.width, params.height, 8, &ppcImgMem,
			&pid);
	if (retInt != IS_SUCCESS) {
		throw UeyeOpenCVException(cameraHandle, retInt);
	}
	cout << "mem alloced" << endl;
	retInt = is_SetImageMem(cameraHandle, ppcImgMem, pid);
	if (retInt != IS_SUCCESS) {
		throw UeyeOpenCVException(cameraHandle, retInt);
	}
	//	//set auto settings
	//	retInt = is_SetAutoParameter(hCam, IS_SET_ENABLE_AUTO_WHITEBALANCE, &on, &empty);
	//	if (retInt != IS_SUCCESS) {
	//		throw UeyeOpenCVException(hCam, retInt);
	//	}
	//	cout<<"white balance set"<<endl;
	retInt = is_SetAutoParameter(cameraHandle, IS_SET_ENABLE_AUTO_GAIN, &on, &empty);
	if (retInt != IS_SUCCESS) {
		throw UeyeOpenCVException(cameraHandle, retInt);
	}
	cout << "auto gain set" << endl;
	retInt = is_CaptureVideo(cameraHandle, IS_WAIT);
	if (retInt != IS_SUCCESS) {
		throw UeyeOpenCVException(cameraHandle, retInt);
	}

	ErrorCode error = SUCCESS;
	return error;

}

ErrorCode UeyeCameraImpl::closeCamera() {

	int retInt = is_ExitCamera(cameraHandle);
		if (retInt != IS_SUCCESS) {
			throw UeyeOpenCVException(cameraHandle, retInt);
		}

	ErrorCode error = SUCCESS;
	return error;

}

ErrorCode UeyeCameraImpl::captureVideo() {
	ErrorCode error = SUCCESS;
	return error;
}
ErrorCode UeyeCameraImpl::stopVideo() {
	ErrorCode error = SUCCESS;
	return error;
}

ErrorCode UeyeCameraImpl::getImageFormat(uint16_t *width, uint16_t *height) {

	*width = (uint16_t)params.width;
	*height = (uint16_t)params.height;

	ErrorCode error = SUCCESS;
	return error;

}

ErrorCode UeyeCameraImpl::setImageFormat(uint16_t *width, uint16_t *height) {
	ErrorCode error = SUCCESS;
	return error;
}

ErrorCode UeyeCameraImpl::setFrameRate(uint16_t *frameRate) {
	ErrorCode error = SUCCESS;
	return error;
}
ErrorCode UeyeCameraImpl::getFrameRate(uint16_t *frameRate) {
	ErrorCode error = SUCCESS;
	return error;
}

ErrorCode UeyeCameraImpl::setExposureTime(uint16_t *exposureTime) {
	ErrorCode error = SUCCESS;
	return error;
}
ErrorCode UeyeCameraImpl::getExposureTime(uint16_t *exposureTime) {
	ErrorCode error = SUCCESS;
	return error;
}

ErrorCode UeyeCameraImpl::setPixelRate(uint16_t *pixelRate) {
	ErrorCode error = SUCCESS;
	return error;
}
ErrorCode UeyeCameraImpl::getPixelRate(uint16_t *pixelRate) {
	ErrorCode error = SUCCESS;
	return error;
}

ErrorCode UeyeCameraImpl::setAutoPameter() {
	ErrorCode error = SUCCESS;
	return error;
}
ErrorCode UeyeCameraImpl::getAllPameter() {
	ErrorCode error = SUCCESS;
	return error;
}

ErrorCode UeyeCameraImpl::getId(uint8_t *id) {
	ErrorCode error = SUCCESS;
	return error;
}

ErrorCode UeyeCameraImpl::resetToDefault() {
	ErrorCode error = SUCCESS;
	return error;
}
string UeyeCameraImpl::getErrors() {

	return "No Errors";
}

DisplayMode UeyeCameraImpl::getDisplayMode() {
	DisplayMode dispMode = DIB_MODE;
	return dispMode;
}
void UeyeCameraImpl::setDisplayMode(DisplayMode dispMode) {

	this->displayMode = dispMode;

}
void UeyeCameraImpl::getFrame(cv::Mat& mat) {
	VOID* pMem;

	int retInt = is_GetImageMem(cameraHandle, &pMem);

	if (retInt != IS_SUCCESS) {
		throw UeyeOpenCVException(cameraHandle, retInt);
	}
//	if (mat.cols == width && mat.rows == height && mat.depth() == 3) {
		memcpy(mat.ptr(), pMem, params.width * params.height );
//	} else {
//		throw UeyeOpenCVException(hCam, -1337);
//	}
}

